package org.kukker.list;

import java.util.Map;

import org.kukker.R;
import org.kukker.activity.IContextMenuWatcher;
import org.kukker.event.Event;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ContextMenu;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnPopulateContextMenuListener;
import android.view.animation.TranslateAnimation;
import android.view.animation.Animation.AnimationListener;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.xml.dom.ElementImpl;

enum ElementMetadata {
	SELECTED ("isSelected","1"),
	UNSELECTED ("isSelected","0");
	
	 
	
	public final String label;   // in kilograms
	public final String value;   // in kilograms
		
	ElementMetadata(String label,String value){
		this.label = label;
		this.value = value;	
	}
	
	public void apply(ElementImpl el){
		el.setAttributeNS(org.kukker.xml.Constants.ns,this.label,this.value);		
	}
}



public final class KukkerListView extends ListView {
	
	boolean showDebugMessage = false;
	
/*
	enum Event{
		onDragScrollStart,
		onDragScrolling,
		onDragScrollEnd;
	}
	*/
	
	private boolean mIsScrolling = false;
	
	
	public boolean mChildItemIsSelected = false;
	private KukkerListItemView 	mLastSelectedItem; 
	private int 				mLastSelectedItemPosition;
	
	public View mChildItemSelected = null;
	
	
	private KukkerListItemView mLastChildAdded;
	public KukkerListItemView mActiveEventView;	
	public KukkerListItemView mLastFocusedEventView;
	public static KukkerListView instance;
	public int mEventViewDeselectedColor = Color.BLACK;
	public int mEventViewSelectedColor = Color.DKGRAY;
	
	public View mLastPressedDownItem;
	public int mLastPressedDownItemPosition;
	
	public boolean mLastPressedDownItemCancelEvent = false;
	public long mTouchMoveLastTime;
	public boolean mTouchMoveFirst = false;
	public float mTouchLastX ;
	private double mTouchMoveVelocityThreshold = 0.3; //dont set too low > 0.2
	
	private org.kukker.list.KukkerListView.Animation animation;

	private ElementImpl mSelectedItem;
	private IKukkerListItemListener mKukkerListItemListener;
	private OnListViewScrollListener mOnListViewScrollListener;
	private OnHierarchyChangeListener mOnHierarchyChangeListener; 
	
	private boolean mIsActivated = false;
	
	//icons
	public static Drawable icon_saved;
	public static Drawable icon_saved_disabled;
	public static Drawable icon_web;
	public static Drawable icon_web_disabled;
	public static Drawable icon_none;
	
	IContextMenuWatcher mContextMenuWatcher;
	
	public void setContextMenuWatcher(IContextMenuWatcher cmw){
		this.mContextMenuWatcher = cmw;
	}
	public IContextMenuWatcher getContextMenuWatcher(){
		return this.mContextMenuWatcher;		
	}
	
	public void setKukkerListItemListener(IKukkerListItemListener listener){
		this.mKukkerListItemListener = listener;
		
	}
	
	/**
	 * Construct object, initializing with any attributes we understand from a
	 * layout file. These attributes are defined in
	 * SDK/assets/res/any/classes.xml.
	 * 
	 * @see android.view.View#View(android.content.Context,
	 *      android.util.AttributeSet, java.util.Map)
	 */
	
	/*
	public void rememberSyncState(){
		Log.i("debug",
    			"["+this.getClass().getSimpleName()+"]" 
    			
    		
    			+"["
    			+"rememberSyncState() " 
			
    			+"]"
    	);
		
	}
	
*/

	private void init(){
		
		//set static icons
		KukkerListView.icon_saved = this.getResources().getDrawable(R.drawable.icon_save);
		KukkerListView.icon_saved_disabled = this.getResources().getDrawable(R.drawable.icon_save_gray_16x16);
		KukkerListView.icon_web = this.getResources().getDrawable(R.drawable.icon_web);
		KukkerListView.icon_web_disabled = this.getResources().getDrawable(R.drawable.icon_web_gray_16x16);
		KukkerListView.icon_none = this.getResources().getDrawable(R.drawable.transparent_dot);
		
		//at initial instantiation disable all events detection of this widget
		boolean isBlockAllEvents = true;
		this.setFocusableInTouchMode(!isBlockAllEvents);
		this.setClickable(!isBlockAllEvents);
		this.setFocusable(!isBlockAllEvents);
		this.setBlockDescendantFocus(isBlockAllEvents);
		
		//		
		//this.setOnPopulateContextMenuListener(this.mOnPopulateContextMenuListener);
	}
	
	private void initLookAndFeel(){
		this.setSelector(R.drawable.greenoverlay_darkside);
		//this.setDrawSelectorOnTop(true);
		//this.setBackground(R.drawable.overlaygrey);
		//this.setBackgroundColor(Color.DKGRAY);
		this.setDivider(null);		
		this.setFadingEdgeLength(4);
	}
	
	private void initListeners(){
		this.setOnItemClickListener(KukkerListView.getDefaultOnItemClickListener());
		
		/*
		this.mOnListViewScrollListener = new OnListViewScrollListener(){
			public void onListViewScroll(int arg0, int arg1, int arg2) {
				 Log.i("eventflow1",
			    			"["+this.getClass().getSimpleName()+"]" +
			    			
			    			"["+
			    			
			    			
			    			"]"
			    			
			    			+"["
			    			+"onListViewScroll()"    			
			    			
			    			
			    			
			    			+"]"
			    			
					 );
				
			}			
		};
		*/

		this.mOnHierarchyChangeListener = new OnHierarchyChangeListener(){

			@Override
			public void onChildViewAdded(View parent, View child) {
				if(showDebugMessage){
					/*
				 Log.i("eventflow2",
			    			"["+this.getClass().getSimpleName()+"]" +

			    			"[]"+
			    			
			    			"[" +
			    			"onChildViewAdded()" +    	
			    			", parentView:"+parent.getClass().getSimpleName() +
			    			", childView:"+child.getClass().getSimpleName() +			    			
			    			"]"
			    			
					 );
					 */
				}
				 
				 //set important attributes to the added view.
				KukkerListItemView v = ((KukkerListItemView)child);
				
				
				v.setKukkerListItemListener(mKukkerListItemListener);
				mLastChildAdded = v;
				
				 
				v.updateView();
				 
				 
			}

			@Override
			public void onChildViewRemoved(View parent, View child) {
				
				if(showDebugMessage){
					/*
				 Log.i("eventflow2",
			    			"["+this.getClass().getSimpleName()+"]" +
			    			
			    			"[]"+
			    			
			    			"[" +
			    			"onChildViewRemoved()" +    	
			    			", parentView:"+parent.getClass().getSimpleName() +
			    			", childView:"+child.getClass().getSimpleName() +			    			
			    			"]"
			    			
					 );*/
				}
				
				 try{
					// KukkerListItemView item = (KukkerListItemView)child;
							 
				 }catch(Exception e){
					 
				 }
				 //eventView.setBackground(d);
				
			}
			
		};
		
		
	}
	
	
	public KukkerListView(Context context, AttributeSet attrs, Map inflateParams) {
		super(context, attrs, inflateParams);
	
		this.init();
		this.initListeners();
		this.initLookAndFeel();
		
		//this.setBlockUnmatchedKeyUps(true);
		
		this.animation = new Animation();
		//TextView tv1 = new TextView(context);
		//tv1.setText("Header");
		
		//this.addHeaderView(tv1);
		//this.setPadding(0,0,0,0);
		//this.setClipChildren(false);
		
		
		/*
		this.setOnItemSelectedListener(new OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView view, View arg1, int position,
					long id) {
				Log.i("eventflow1",
		    			"["+this.getClass().getSimpleName()+"]" 
		    			
		    		
		    			+"["
		    			+"onItemSelected() " 
	    			
		    			+"]"
		    	);
				
				retrieveSelectedItem();
				if(mSelectedItem != null){
				boolean isSelected = isItemDataSelected(mSelectedItem);
				//toggle				
				if(isSelected){unselectItem();}
				}
			}

			@Override
			public void onNothingSelected(AdapterView arg0) {			
				
			}
			
		});
			
		*/
		
		this.setOnListViewScrollListener(this.mOnListViewScrollListener);
		this.setOnHierarchyChangeListener(this.mOnHierarchyChangeListener);
		
		//this.setScrollingCacheEnabled(true);
		
		

		
	}

	/*
	
	public void handleDataChanged(){
		 Log.i("eventflow2",
	    			"["+this.getClass().getSimpleName()+"]" +
	    			
	    			"["+
	    			"isSelected:"+this.isSelected()+
	    			"]"
	    			
	    			+"["
	    			+"handleDataChanged()"
	    			
	    			
	    			+"]"
	    			
			 );		
		 
		 super.handleDataChanged();
		 
		 
	}
	*/
	
	public void setLastPressedDownItem(View v){
		this.mLastPressedDownItem = v;
	}
	/*
	public void setLastPressedDownItemPosition(int pos){
		this.mLastPressedDownItemPosition = pos;
	}*/
	
	public int getLastPressedDownItemPosition(){
		return this.mMotionPosition; //hack: the key for enabling context menu.
		//return this.mLastPressedDownItemPosition;
	}
	
	public void setLastPressedDownItemCancelEvent(boolean state){
		this.mLastPressedDownItemCancelEvent = state;
	}
	
	public boolean getLastPressedDownItemCancelEvent(){
		return this.mLastPressedDownItemCancelEvent;
	}
	
	
	public void scrollDown() {
		this.arrowScroll(ListView.FOCUS_DOWN);
	}

	public void scrollUp() {
		this.arrowScroll(ListView.FOCUS_UP);
	}

	public void onScrollChanged(int l, int t, int oldl, int oldt) {
		Log.i("eventflow1", this.getClass().getSimpleName() + " ScrollChanged");
		
		
		

	}
	
	private void resetTouchMoveVariables(){
		setLastPressedDownItem(null);
		setLastPressedDownItemCancelEvent(false);
		this.mTouchMoveFirst = true;
	}
	
	public void trackMotionScroll(int deltaY, int incrementalDeltaY){
		if(this.showDebugMessage){
		 Log.i("eventflow1",
	    			"["+this.getClass().getSimpleName()+"]" +
	    			
	    			"["+
	    			"isSelected:"+this.isSelected()+
	    			"]"
	    			
	    			+"["
	    			+"trackMotionScroll()"
	    			
	    			+", deltaY:"+deltaY
	    			+", incrementalDeltaY:"+incrementalDeltaY
	    			+"]"
	    			
			 );
		}
		 super.trackMotionScroll(deltaY, incrementalDeltaY);
		
	}
	
	public boolean detectGesture1(MotionEvent ev){
		long now = System.currentTimeMillis();
		long elapsed = now - mTouchMoveLastTime;
		float dist = ev.getX() - mTouchLastX;
		mTouchLastX = ev.getX();
		float speed = dist / elapsed;
		Log.i("eventflow1", "speed:" + speed);
		mTouchMoveLastTime = now;
		if (speed > this.mTouchMoveVelocityThreshold) {
			this.resetTouchMoveVariables();
			return true;
		}
		return false;
	}
	
	//TODO Refactor gesture or rethink design.
	public void doGesture1(){
		Log.i("Gesture","doGesture1");
		/*
			EventView.Animation animFact = (EventView.Animation) EventView.Animation.getInstance();
			TranslateAnimation translateAnim = (TranslateAnimation) animFact
					.getFloatingMenuVisibleAnimation();

			// change event box color						
			// start floating menu animation
			*/
		
			//FloatingMenu.getInstance().startAnimation(translateAnim);
	}
	
	
	public boolean onTouchEvent(MotionEvent ev){
		//boolean handled = super.onTouchEvent(ev);
		boolean res = super.onTouchEvent(ev);

		
		String actionName = org.kukker.Util.kMotionEvent.getActionName(ev);
		
		if(this.showDebugMessage){
		Log.i("eventflow1",
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			
    			"]"
    			
    			+"["
    			
    			+"onTouchEvent()"  
    		
    			+", action:"+actionName
    			+", isFocused:"+this.isFocused()  
    			+", isSelected:"+this.isSelected()
    			+", isScrolling:"+this.mIsScrolling
    			+", mLastPressedDownPos:"+this.mLastPressedDownItemPosition
    			+", mSelectedPosition:"+this.mSelectedPosition
    			+"]"
    			
		 );
		}
		 
		 
		/* default super implementation:
		 *  if Up:		will select the underlying item.
		 *  if Move:    will auto motion scroll
		 *    
		 */
		
		int action = ev.getAction();
	//	if(action == MotionEvent.ACTION_MOVE){
			
		//}
		
		if(action == MotionEvent.ACTION_DOWN){
			return true;
		}
		
		else if(action == MotionEvent.ACTION_UP){			
			if(this.mIsScrolling){
				this.mIsScrolling = false;			
				if(this.mLastChildAdded!=null){
					this.mLastChildAdded.setSelected(true);
					this.mLastChildAdded.setFocusableInTouchMode(true);
					this.setSelected(false);
					
					//AUTO SELECT THE LATEST ADDED ITEM
					this.setSelection(mLastChildAdded.getPosition());
					
					//
					//this.mLastChildAdded.requestFocus();					
					//
					
				}
			
			}else{
				super.onTouchEvent(ev);
				
				boolean isGesture1 = this.detectGesture1(ev);
				if(isGesture1){
					this.doGesture1();
				}else{
					this.resetTouchMoveVariables();
				}
				
				//transfer focus to child 
				int pos = this.getLastPressedDownItemPosition();
				this.setSelection(pos);
				
				return false;
			}
		}
		
		
		else if (action == MotionEvent.ACTION_MOVE) {
			//res = super.onTouchEvent(ev);
			
			this.mIsScrolling = true; 
			//assumed, since the child had to be in ACTION_CANCEL 
			
			if(this.showDebugMessage){
			Log.i("eventflow1", "[EventListView]"+" move");
			}
			return res;
			//assume drag-scrolling mode
			
			/*
			//get the child in focus and clear its background
			{
			View aView = this.findFocus();
			if(aView != null){
				Log.i("debug","clearing focus");
				//((EventView)aView).clearFocus();
				try{
				//((EventView) aView).setBackground(Background.UNSELECTED.DrawableResourceId);
				}catch(Exception e){
					
				}
			}
			}
			
			long now = System.currentTimeMillis();
			if(mTouchMoveFirst){
				mTouchMoveLastTime = now;
				mTouchMoveFirst = false;
			}
			else{				
				//TODO: refactor
				boolean isGesture1 = this.detectGesture1(ev);
				if(isGesture1){
					this.resetTouchMoveVariables();
					this.doGesture1();
				}else{
					// nothing
				}
			}		
			*/
			
		}
		
		return true;
	}
	
	
	/*
	public KukkerListItemView getLastSelectedItem(){
		return this.mLastSelectedItem;
	}
	*/
	
	/**
	 * Returns the position of the child item that was pressed down
	 * Note that the value returned is different from when selection changed because of key events.
	 * Hack for context menu, since press and hold will change selection to -1
	 * @return
	 */
	public int getLastSelectedItemPosition(){
		return this.mLastSelectedItemPosition;
	}
	
	/**
	 * is called only when a different item is selected
	 * selection happens when a child item is touched. 
	 */
	protected void selectionChanged(){
		if(showDebugMessage){		
			Log.i("debug",this.getClass().getSimpleName()+" selection changed, position: "+this.getSelectedItemPosition());
		}
		/*
		int selection = this.getSelectedItemPosition();
		if(selection >= 0){
			this.mLastSelectedItemPosition = selection;
		}
		if(this.mLastSelectedItemPosition >= 0){
			try{
				Object obj = this.getSelectedItem();
				if(obj != null){					
					this.mLastSelectedItem = (KukkerListItemView) obj;		
				}
			 
			}catch(Exception e){}
		}
		*/
		
		
		/*
		
		//transfer focus to the view child
		View v = this.getChildAt(this.mLastSelectedItemPosition);
		if(v!=null){
			v.requestFocus();
		}
		*/
		
		
		
	}

	public void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect) {
		
		if(showDebugMessage){
		
		Log.i("eventflow1",
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			
    			"]"
    			
    			+"["
    			+"onFocusChanged()"
    			
    			+", gainFocus:"+gainFocus
    			+", isSelected:"+this.isSelected()
    			+", direction:"+direction    		
    			+" []" 
    			+", isScrolling:"+this.mIsScrolling
    			
    			+"]"
    			
    	);
		}
		
		if(gainFocus && !this.mIsActivated){
						
			this.mIsActivated = true;
			if(this.mLastSelectedItem != null){
				this.setFocusable(false); //ADDED: for bug fix: where you can't drag scroll after going back to the search result from the event detail.
				//VERY important note: first we set focusable false for the listview to prevent the "event bounce" effect.
				
				
				//when we call the child.requestFocus(), the parent's onFocusChanged(gainFocus:true) is invoked
				//then at the last end, the parent's onFocusChanged(gainFocus:false) is invoked.
				/*
				 * 
				 *
INFO/eventflow1(7647): [EventDetailView][isSelected:false][onFocusChanged(), gainFocus:false, direction:0]
INFO/eventflow1(7647): [KukkerListView][][onFocusChanged(), gainFocus:true, isSelected:true, direction:130 [], isScrolling:false]
INFO/eventflow1(7647): [UpcomingEventView][][onFocusChanged(), position:2, gainFocus:true, isSelected:true, direction:130]value: Frank Paul Inquiry Rally
INFO/eventflow1(7647): [KukkerListView][][onFocusChanged(), gainFocus:false, isSelected:true, direction:0 [], isScrolling:false]

				 *  The last time is a bounce
				 * 
				 */
				this.setSelection(this.mLastSelectedItemPosition);
				this.mLastSelectedItem.requestFocus();
				
				this.setFocusable(true);// and then re-enable the focus
			}
			return;
			
		}
		
		if (gainFocus) {
			if(this.mChildItemIsSelected && !this.mIsScrolling){				
				 if(this.mChildItemSelected != null){
					 Log.i("debug",((KukkerListItemView)mChildItemSelected).getPosition()+"");
					 this.mChildItemSelected.requestFocus();
					 this.mChildItemSelected.setSelected(true);
				 }
			}
			
			if(!this.mIsScrolling){				
				 if(this.mChildItemSelected != null){
					 Log.i("debug",((KukkerListItemView)mChildItemSelected).getPosition()+"");
					 this.mChildItemSelected.requestFocus();
					 this.mChildItemSelected.setSelected(true);
				 }
			}
			/*
			//select the last selected item 
			this.setSelection(this.mLastSelectedItemPosition);
						
			this.setFocusable(false);
			boolean focusTaken = this.requestFocus(View.FOCUS_DOWN);
			
			
			View whoHasFocus = this.findFocus();
			if(whoHasFocus != null){
				//EventView view2 = (EventView)whoHasFocus;			
				Log.i("eventflow1", "focus is taken by:" + whoHasFocus.toString()
				+", focusTaken:"+focusTaken		
				);
						//+", position:"+view2.getPosition());
			}
			*/
			
			/*
			View view2 = this.focusSearch(this, View.FOCUS_DOWN);
			if (view2 != null) { // null is possible when no EventView object
									// is instantiated.
				Log.i("eventflow1", "giving focus to:" + view2.toString()+", position:"+((KukkerListItemView)view2).getPosition());

				view2.requestFocus();
				//this.clearFocus();
			
			}
			*/
			
		} else {
			//this.setFocusable(false);
		}

	}

	public static KukkerListView getInstance() {
		return KukkerListView.instance;
		
		
	}

	
	/**
	 * Selects this item
	 */
	private void selectItem(){		
		if(mSelectedItem != null)
		ElementMetadata.UNSELECTED.apply(mSelectedItem);
	}
	
	/**
	 * De-selects this item
	 */
	private void unselectItem(){
		if(mSelectedItem != null)
		ElementMetadata.UNSELECTED.apply(mSelectedItem);
				
		
	}
	
	public void setDetectIncomingEvents(boolean state){
		//
		this.setClickable(state);
		this.setFocusable(state);
		this.setFocusableInTouchMode(state);
		this.setBlockChildEvents(!state);
		this.setBlockDescendantFocus(!state);//blocks or ignoring focus from a key down
		this.setEnabled(state);
		
		if(!state){			// clear item view that has focus so that it wont receive key events
			View v = this.findFocus();
			if(v!=null){v.clearFocus();}
		}
		//
	}
	
	public void storeLastSelectedItem(){
		this.mLastSelectedItem = (KukkerListItemView)this.getSelectedView();
		if(this.mLastSelectedItem != null){this.mLastSelectedItemPosition =  this.mLastSelectedItem.getPosition();}
	}
	
	
	public void setLastSelectedItemPosition(int pos){
		this.mLastSelectedItemPosition = pos;
	}
	
	public void setLastSelectedItem(KukkerListItemView v){
		this.mLastSelectedItem = v;
	}
		
	private boolean isItemDataSelected(ElementImpl el){
		String value = el.getAttributeNS(org.kukker.xml.Constants.ns, ElementMetadata.SELECTED.label);
		if(value != null){		
			return (value.compareTo(ElementMetadata.SELECTED.value) == 0);
		}else{
			return false;
		}
	}
	
	public boolean onKeyDown(int keyCode,KeyEvent event){
		super.onKeyDown(keyCode, event);
		
		if(showDebugMessage){
		Log.i("eventflow1",
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			
    			"]"
    			
    			+"["
    			+"onKeyDown() " 
    			
    			+", keyCode:"+keyCode
    			+", isFocused:"+this.isFocused()
    			+", isSelected:"+this.isSelected()
    			+", repeat:"+event.getRepeatCount()
    			+"]"    			
    	);
		
		} 
		
		/*
		//this.clearFocus();
		View v = this.focusSearch(View.FOCUS_DOWN);
		if(v!=null){
			v.requestFocus();
		}
		*/
		
		return false;
		
	}
	
	
	public boolean onKeyUp(int keyCode,KeyEvent event){
			
		super.onKeyUp(keyCode, event); //if not called, the list will not auto scroll, super implementation provides auto scrolling
		if(this.showDebugMessage){
		Log.i("eventflow1",
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			
    			"]"
    			
    			+"["
    			
    			+"onKeyUp() " 
    			
    			+", keyCode:"+keyCode    		
    			+", isFocused:"+this.isFocused()    	
    			+", isSelected:"+this.isSelected()
    			+", repeat:"+event.getRepeatCount()
    			+"]"    			
    	);
		}
		//this.clearFocus();
		
		
		
		return false;		
	}
	
	
	public static OnItemClickListener getDefaultOnItemClickListener(){
		
        OnItemClickListener oicl = new OnItemClickListener(){
        	public void onItemClick(AdapterView parent, View v, int position, long id){
        		Log.i("eventflow1",
            			"["+this.getClass().getSimpleName()+"]" +
            			
            			"["+
            			
            			"]"
            			
            			+"["            			
            			+"onItemClick() "
            			+", view:"+v.getClass().getSimpleName()
            			+", id:"+id
            			+", position:"+position
            			+"]"    			
            	);
        		
        		Log.i("DEBUG","onListItemCLick:"+position);
        		Adapter adapter = parent.getAdapter();
        		KukkerListAdapter ela = (KukkerListAdapter) adapter;
        		if(ela == null){
        			Log.i("DEBUG","ERROR");
        		}else{	
        			//ela.toggle(position);
        		
        		}
        	}
        };
        
        return oicl;
		
	}
	
	public void setActivated (boolean state){
		this.mIsActivated = state;
	}
	
	public void awakenScrollbars(int time){
		super.awakenScrollBars(time);
	}
	

	public void clearAllFocus(){
		this.setFocusable(false);
		this.setBlockDescendantFocus(true);
		this.setBlockChildEvents(true);
		this.setClickable(false);
		this.setItemsCanFocus(false);
		this.clearFocus();
		
		
		
		
		View v  = this.findFocus();
		if(v!=null){
			v.clearFocus();
		}
		this.clearFocus();
		
	}
	public void regainAllFocus(){
		this.setFocusable(true);
			this.setBlockDescendantFocus(false);
			this.setBlockChildEvents(false);
			this.setClickable(true);
			this.setItemsCanFocus(true);
			this.requestFocus();
	}
	

	
	
	public void detachViewFromParent(View view){
		super.detachViewFromParent(view);
		
		Log.i("debug1",
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			
    			"]"
    			
    			+"["            			
    			+"detachViewFromParent() "
    			+", view:"+view.getClass().getSimpleName()
    		//	+", id:"+id
    			//+", position:"+position
    			+"]"    			
    	);
		
	}
	
	public void detachViewFromParent(int index){
		super.detachViewFromParent(index);
		
		
		Log.i("debug1",
    			"["+this.getClass().getSimpleName()+"]" +
    			
    			"["+
    			
    			"]"
    			
    			+"["            			
    			+"detachViewFromParent(int) "
    		//	+", view:"+view.getClass().getSimpleName()
    		//	+", id:"+id
    			//+", position:"+position
    			+"]"    			
    	);
		
		
		
	}
	
	public TranslateAnimation getAnimHideRight(AnimationListener animationListener){
		
		TranslateAnimation hideRight = this.animation.hideRight();
		hideRight.setAnimationListener(animationListener);
		this.startAnimation(hideRight);
		
		return hideRight;
	}
	
	
	private boolean mBlockChildEvents = false;
	public void setBlockChildEvents(boolean val){
		mBlockChildEvents = true;
	}
	
	public boolean onInterceptTouchEvent(MotionEvent ev){
		if(mBlockChildEvents){
			//Log.i("debug","mLastY:"+this.mMotionPosition);			
			return true;
		}		
		else return super.onInterceptTouchEvent(ev);		
	}
	
	
	static final class Animation {
		
		//TODO: static variables for storing screen dimension for animation to work properly.
		private static int screenWidth = 320;		
		
		public static TranslateAnimation hideRight(){			
				long animationDuration = 1000;
				TranslateAnimation translateAnim;
				translateAnim = new TranslateAnimation(0,screenWidth,0,0);
				translateAnim.setDuration(animationDuration);
				return translateAnim;			
		}
		
		public static TranslateAnimation showRight(){			
			long animationDuration = 1000;
			TranslateAnimation translateAnim;
			translateAnim = new TranslateAnimation(-screenWidth,0,0,0);
			translateAnim.setDuration(animationDuration);
			return translateAnim;			
	}
		
		
	}
	
}